package com.hjy.scheduled.config.service.impl;

import com.hjy.scheduled.config.redis.CacheClient;
import com.hjy.scheduled.config.service.RedisReadWriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RedisReadWriteServiceImpl implements RedisReadWriteService {

    @Autowired
    private CacheClient cache;

    /**
     * @Title: get
     * @Description: 读取Redis值
     * @param prefix 后缀
     * @param key 健
     * @return T
     */
    @SuppressWarnings("unchecked")
    @Override
    public <T> T get(String prefix, String key) {
        if (null == prefix || null == key) {
            return null;
        }
        return (T) cache.get(prefix, key);
    }

    /**
     * @Title: set
     * @Description: 写入Redis值
     * @param prefix 后缀
     * @param key 健
     * @param t 写入值
     * @return boolean
     */
    @Override
    public <T> boolean set(String prefix, String key, T t) {
        if (null == prefix || null == key) {
            return false;
        }
        return cache.set(prefix, key, t);
    }

    /**
     * @Title: set
     * @Description: 写入Redis值
     * @param prefix 后缀
     * @param key 健
     * @param t 写入值
     * @param timeout 值失效时间
     * @return boolean
     */
    @Override
    public <T> boolean set(String prefix, String key, T t, Long timeout) {
        if (null == prefix || null == key) {
            return false;
        }
        return cache.set(prefix, key, t, timeout);
    }

    /**
     * @Title: delete
     * @Description: 删除Redis值
     * @param prefix 后缀
     * @param key 健
     * @return boolean
     */
    @Override
    public <T> boolean delete(String prefix, String key) {
        if (null == prefix || null == key) {
            return false;
        }
        cache.delete(prefix, key);
        return true;
    }

    /**
     * @Title: getList
     * @Description: 读取Redis值-LIST
     * @param prefix 后缀
     * @param key 健
     * @return List<T>
     */
    @Override
    public <T> List<T> getList(String prefix, String key) {
        if (null == prefix || null == key) {
            return null;
        }
        return cache.getList(prefix, key);
    }

    /**
     * @Title: setList
     * @Description: 写入Redis值-LIST
     * @param prefix 后缀
     * @param key 健
     * @param t LIST
     * @return boolean
     */
    @Override
    public <T> boolean setList(String prefix, String key, List<T> t) {
        if (null == prefix || null == key) {
            return false;
        }
        return cache.setList(prefix, key, t);
    }

    /**
     * @Title: hashGet
     * @Description: 读取Redis值-HASH
     * @param realKey 外层大健
     * @param hashKey 内层小健
     * @return T
     */
    @SuppressWarnings("unchecked")
    @Override
    public <T> T hashGet(String realKey, String hashKey) {
        if (null == realKey || null == hashKey) {
            return null;
        }
        return (T) cache.hashGet(realKey, hashKey);
    }

    /**
     * @Title: hashSet
     * @Description: 写入Redis值-HASH
     * @param realKey 外层大健
     * @param hashKey 内层小健
     * @param t
     * @return boolean
     */
    @Override
    public <T> boolean hashSet(String realKey, String hashKey, T t) {
        if (null == realKey || null == hashKey) {
            return false;
        }
        return cache.hashSet(realKey, hashKey, t);
    }

    /**
     * @Title: hashDelete
     * @Description: 删除Redis值-HASH
     * @param realKey 外层大健
     * @return boolean
     */
    @Override
    public <T> boolean hashDelete(String realKey) {
        if (null == realKey) {
            return false;
        }
        return cache.hashDelete(realKey);
    }

    /**
     * @Title: hashDelete
     * @Description: 删除Redis值-HASH
     * @param realKey 外层大健
     * @param hashKey 内层小健
     * @return boolean
     */
    @Override
    public <T> boolean hashDelete(String realKey, String hashKey) {
        if (null == realKey || null == hashKey) {
            return false;
        }
        return cache.hashDelete(realKey, hashKey);
    }
}
